﻿using Common.Contract.Authentication.Record;
using Common.Contract.HuntingCustomer.Record;
using Common.Contract.HuntingCustomer.Request;
using Common.Contract.HuntingCustomer.Response;
using Common.Service.Command;
using SalePerformance.Model.Interface;
using SalePerformance.Model.Model;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SalePerformance.Core.ServiceCommand.HuntingCustomer
{
    public class RetrieveRelationCustomerInfoByLeadIdServiceCommand : ProviderServiceCommand<IProviderFactory, RetrieveRelationCustomerInfoByLeadIdRequest, RetrieveRelationCustomerInfoByLeadIdResponse>
    {
        #region Properties
        public UserRecord Dao;
        public string TextSearch;
        public int pageSize = 10, pageIndex = 0, totalRecord = 0;
        public DateTime? FromDate;
        CultureInfo cul = CultureInfo.GetCultureInfo("vi-VN");   // try with "en-US"
        #endregion
        public RetrieveRelationCustomerInfoByLeadIdServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(RetrieveRelationCustomerInfoByLeadIdRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (request.CurrentUser == null)
            {
                throw new ArgumentException("MSBO Dao must be set", "request");
            }
            else
            {
                Dao = Request.CurrentUser;
            }
        }

        protected override bool OnExecute(RetrieveRelationCustomerInfoByLeadIdResponse response)
        {
            bool isSuccess = true;
            string errorMessage = string.Empty;
            try
            {
                CustomerHuntingRecord record = new CustomerHuntingRecord();
                TblHuntingCustomer listCustomer = new TblHuntingCustomer();
                listCustomer = Providers.HuntingCustomer.RetrieveCustomerHuntingByLeadId(long.Parse(Request.LeadId));
                if (listCustomer != null)
                {
                    record =  new CustomerHuntingRecord
                    {
                        CompanyAddress = listCustomer.CompanyAddress,
                        Createtime = listCustomer.Createtime,
                        UserDaoAssign = listCustomer.UserDaoAssign,
                        UserDaoName = listCustomer.UserDaoName,
                        BranchCode = listCustomer.BranhCode,
                        BranchCodeSme = listCustomer.BranchCodeSme,
                        BranchName = listCustomer.BranchName,
                        BranchNameMap = listCustomer.BranchNameMap,
                        BranchNameSme = listCustomer.BranchNameSme,
                        CompanyEmail = listCustomer.CompanyEmail,
                        CompanyInfo = listCustomer.CompanyInfo,
                        CompanyName = listCustomer.CompanyName,
                        CompanyPhone = listCustomer.CompanyPhone,
                        ContactStatus = listCustomer.ContactStatus,
                        Dao = listCustomer.Dao,
                        DaoName = listCustomer.DaoName,
                        Leadid = listCustomer.Leadid,
                        LeadSource = listCustomer.LeadSource,
                        LeadStatus = listCustomer.LeadStatus,
                        LegalId = listCustomer.LegalId,
                        Modifiedtime = listCustomer.Modifiedtime,
                        ProductCategory = listCustomer.ProductCategory,
                        ProductName = listCustomer.ProductName,
                        ProductNo = listCustomer.ProductNo,
                        Region = listCustomer.Region,
                        RegionMap = listCustomer.RegionMap,
                        RelationEmail = listCustomer.RelationEmail,
                        RelationName = listCustomer.RelationName,
                        RelationPhone = listCustomer.RelationPhone
                    };
                }
                response.Record = record;
            }
            catch (Exception e)
            {
                isSuccess = false;
                errorMessage = e.Message;
            }

            response.ErrorMessage = errorMessage;
            return isSuccess;
        }
    }
}
